package memory.net;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.util.LinkedList;

import javax.swing.JLabel;
import javax.swing.JPanel;

import memory.Card;
import memory.CardPanel;
import memory.MainWindow;
import memory.Player;
import memory.PlayerPanel;

@SuppressWarnings("serial")
public class NetEventListener extends JPanel implements ActionListener {
	private PlayerPanel playerPanel;
	private CardPanel cardPanel;
	private ClientThread clientThread;
	private ClientLobby clientLobby;
	
	private ObjectDB objectDB = new ObjectDB();
	
	//private GridBagConstraints c = new GridBagConstraints();
	
	private boolean gameRunning = false;
	
	//Temp storage for incoming cards
	LinkedList<Card> cardsTemp = new LinkedList<Card>();	
	
	
	public NetEventListener(ClientLobby cL) {
		clientLobby = cL;
/*		add(new CardPanel());
		String[] mekk = {"herde","mekk"};
		add(new PlayerPanel( mekk ));*/
		
//		setLayout(new GridBagLayout());
	}
	
	public NetEventListener() {
	//	setLayout(new GridBagLayout());
	}
	
	//THIS NEEDS TO BE SET!!
	public void setClientThread(ClientThread cT) {
		clientThread = cT;
	}
	
	public void actionPerformed( ActionEvent e ) {
		
		//We need to check if we've got the reference to the client threads
		if( clientThread != null && e.getSource() instanceof Card ) {
			Card source = (Card)e.getSource();
			Integer id = source.getID();
						
			//Add the card to the send queue.
			clientThread.addToQueue(new WrapperClass("Chosen Card",id));
			
			//Card chosenCard = findCorresponding(source);
			//System.out.println()
			
		}
		
	}
	
	public void newCardPanel(CardPanel cP) {
		//Remove old one
		if(cardPanel != null) 
			remove(cardPanel);
			
		//Assign and add new one
		cardPanel = cP;
		
		//Add cards to DB
		for(int i = 0 ; i < cardPanel.getNrOfCards() ; i++) {
			objectDB.add(cP.getCard(i).getID(),cP.getCard(i));
			cardPanel.getCard(i).add(new JLabel(String.valueOf(cP.getCard(i).getID())));
		}
		
	//	c.gridx = 0;
	//	c.gridy = 0;
		add(cardPanel);
	}
	
	public void newPlayerPanel(PlayerPanel pP) {
		//Remove old one
		if(playerPanel != null)
			remove(playerPanel);
		
		//Assign and add new one
		playerPanel = pP;

		//Add players to DB
		for(int i = 0 ; i < playerPanel.nrOfPlayers() ; i++)
			objectDB.add(pP.getPlayer(i).getID(),pP.getPlayer(i));
		
	//	c.gridy = 1;
		add(playerPanel);
		
	}
	
	public void newChatMsg(String s) {
		clientLobby.addChatMsg(s);
	}
	
	public boolean amIReady() {
		if(cardPanel != null && playerPanel != null) 
			return true;
		return false;
	}
	
	//Don't think we need arguments for this method....
	public void gameRunning(boolean b) {
		gameRunning = b;
		if(b) {
			clientLobby.createMainWindow();
			clientLobby.dispose();
		}
		
	}
	
	private Card findCorresponding(Card fromServer) {
		//Find the card corresponding to our own. Use the ID as identifier.
		int i = 0;
		while( cardPanel.getCard(i).getID() != fromServer.getID() ) {
			i++;
		}
		
		return cardPanel.getCard(i);		
	}
	
	private Player findCorresponding(Player fromServer) {
		//Find the player corresponding to our own. Use the ID as identifier.
		int i = 0;
		while( playerPanel.getPlayer(i).getID() != fromServer.getID() ) {
			i++;
		}
		
		return playerPanel.getPlayer(i);
	}

	public void showCard(Integer id) {
		Card c = (Card)objectDB.get(id);
		c.setStatus(Card.State.SYNLIGT);		
	}
	
	public void hideCard(Integer id) {
		Card c = (Card)objectDB.get(id);
		c.setStatus(Card.State.DOLT);		
	}
	
	public void claimCard(Integer id) {
		Card c = (Card)objectDB.get(id);
		c.setStatus(Card.State.TAGET);
	}
	
	public void setCurrentPlayer(Player p) {
		p = findCorresponding(p);
		playerPanel.setCurrentPlayer(p);		
	}
	
	public void updatePlayer(Player p) {
		//Switch players, litterally
		playerPanel.updatePlayer(p);
	}
	
	public void updatePlayerList(String s) {
		clientLobby.updatePlayerList(s);
	}
	
	public void disconnectedPlayer(Player p) {
		p = findCorresponding(p);
		if(gameRunning)
			p.setBackground(Color.BLACK);
		else {
		//	clientLobby.deletePlayer(p.getPlayerName());
		}
	}
	
	public void hideAllCards() {
		for(int i = 0 ; i < cardPanel.getNrOfCards() ; i++ ) {
			if(cardPanel.getCard(i).getStatus() != Card.State.TAGET)
				cardPanel.getCard(i).setStatus(Card.State.DOLT);
		}
	}
	
	public void connectListeners() {
		for(int i = 0 ; i < cardPanel.getNrOfCards() ; i++ ) {
			if(cardPanel.getCard(i).getStatus() != Card.State.TAGET)
				cardPanel.getCard(i).addActionListener(this);
		}
	}
	
	public void disconnectListeners() {
		//Disconnect all cards until the next turn
		for(int i = 0 ; i < cardPanel.getNrOfCards() ; i++ ) {
			if(cardPanel.getCard(i).getStatus() != Card.State.TAGET)
				cardPanel.getCard(i).removeActionListener(this);
		}
	}	
	

}
